import wx from 'weixin-js-sdk'
import api from '@/api'

// 需要调用的jsapi
const wxAppid = 'wx657efb80ea803a45' // 微信公众号appID
const appsecret = '025c8bcf8c8c1f4f8298a32c12c48b59' // 微信公众号appSecret
const ApiList = [
  'checkJsApi',
  'onMenuShareTimeline',
  'onMenuShareAppMessage',
  'onMenuShareQQ',
  'onMenuShareWeibo',
  'onMenuShareQZone',
  'hideMenuItems',
  'showMenuItems',
  'hideAllNonBaseMenuItem',
  'showAllNonBaseMenuItem',
  'translateVoice',
  'startRecord',
  'stopRecord',
  'onVoiceRecordEnd',
  'playVoice',
  'onVoicePlayEnd',
  'pauseVoice',
  'stopVoice',
  'uploadVoice',
  'downloadVoice',
  'chooseImage',
  'previewImage',
  'uploadImage',
  'downloadImage',
  'getNetworkType',
  'openLocation',
  'getLocation',
  'hideOptionMenu',
  'showOptionMenu',
  'closeWindow',
  'scanQRCode',
  'openProductSpecificView',
  'openCard'
]

export default function () {
  let timestamp = parseInt(new Date().getTime() / 1000)
  // let openurl = encodeURIComponent(location.href.split('#')[0])
  let NONCESTR = 'NONCESTR'
  api.jssdk(wxAppid, appsecret).then(res => {
    console.log(1121212)
    window.localStorage.setItem('wxToken', res.access_token)
    wx.config({
      appId: wxAppid,
      timestamp: timestamp,
      nonceStr: NONCESTR,
      // signature: res.data.signature,
      jsApiList: ApiList
    })
    wx.ready(() => {
      wx.checkJsApi({
        jsApiList: [
          'onMenuShareAppMessage',
          'onMenuShareTimeline',
          'getNetworkType',
          'chooseImage',
          'previewImage',
          'uploadImage',
          'downloadImage'
        ],
        success: function (res) {
          console.log(res)
        }
      })
    })
    wx.error(res => {
      console.log(res)
    })
  }).catch((err) => {
    console.log(err)
  })
}
